package admin

import (
	"night-school-api/models"
	"night-school-api/utils"
	"night-school-api/database"

	"github.com/labstack/echo/v4"
)


func GetUsers(c echo.Context) error {
	// adminUid := c.Get("uid").(int)

	keywords := c.QueryParam("keywords")
	phone := c.QueryParam("phone")

	db := database.DB
	query := db.Model(&models.User{})

	if keywords != "" {
		query = query.Where("name LIKE ?", "%" + keywords + "%")
	}
	if phone != "" {
		query = query.Where("phone = ?", phone)
	}

	var total int64
	var users []models.User
	query.Count(&total)
	page, size := utils.GetPagination(c)
	query.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(&users)
	
	return utils.JSONPagination(c, page, size, total, users)
}